<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_Tests extends ZF_DB{
    protected $_table_name = "tests" ;
    protected $_table_doctor = "doctor";
    protected $_table_test_doctor = "test_doctor";
    protected $_table_hospital = "hospital_detail";
    protected $_table_rate = "rate";
    
    protected $_id;
    protected $_name;
    protected $_location;
    protected $_images;
    protected $_sumary;
    protected $_description;
    protected $_overall;
    protected $_reviews;
    protected $_active;
    
    public function getName(){
        return $this->_name;
    }
    public function setName($name){
        $this->_name = $name;
    }
    
    public function getOverall(){
        return $this->_overall;
    }
    public function setOverall($overall){
        $this->_overall = $overall;
    }
    
    public function getReviews(){
        return $this->_reviews;
    }
    public function setReviews($reviews){
        $this->_reviews = $reviews;
    }
    
    public function getLocation(){
        return $this->_location;
    }
    public function setLocation($location){
        $this->_name_en = $location;
    }
    
    public function getActive(){
        return $this->_active;
    }
    public function setActive($active){
        $this->_active = $active;
    }
    
    public function getImages(){
        return $this->_active;
    }
    public function setImages($images){
        $this->_images = $images;
    }
    
    public function getSumary(){
        return $this->_sumary;
    }
    public function setSumary($sumary){
        $this->_sumary = $sumary;
    }
    
    public function getDesc(){
        return $this->_description;
    }
    public function setDesc($desc){
        $this->_description = $desc;
    }
    
    public function insert($name, $location, $active){
        
        if($name && $location){             
                $data =  array(
                    'name' =>$name,
                    'location'=>$location,
                    'active'=>$active);
            return $this->db->insert($this->_table_name,$data);
	
	}
        return -1;
	
    }
   
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
                    'name' =>$this->_name,
                    'location'=>$this->_location,
                    'active'=>$this->_active);
        return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    
    public function updateOverall($id, $overall, $reviews){
        $where  = "id = " .$id;
	$data =  array(
                    'overall' =>$overall,
                    'reviews'=>$reviews);
        return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    
    public function getById($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name),
                                array("test_name"=>"p.name",
                                    "test_location"=>"p.location",
                                    "test_images"=>"p.images",
                                    "test_sumary"=>"p.sumary",
                                    "test_description"=>"p.description",
                                    "test_overall"=>"p.overall",
                                    "test_reviews"=>"p.reviews",
                                    "hospital_name"=>"p3.name",
                                    "hospital_id"=>"p3.id",
                                    "hospital_address"=>"p3.address",
                                    "doctor_id"=>"p2.id",
                                    "doctor_name"=>"p2.name",
                                    "doctor_title"=>"p2.title",
                                    "doctor_images"=>"p2.images",
                                    "doctor_location"=>"p1.position"))
                        ->join(array("p1"=>$this->_table_test_doctor),
                                        "p.id = p1.id_test")
                        ->join(array("p2"=>$this->_table_doctor),
                                        "p1.id_doctor = p2.id")
                        ->join(array("p3"=>$this->_table_hospital),
                                        "p.id_hospital = p3.id")
                        ->where("p.id = ?",$id );
		
		$data = $this->db->fetchAll($select);
		return $data;
	}
	return -1;
    }
    
    public function getByIdHospitalTestFee($id_hospital, $id_test, $fee){
        if($fee)
	{
            if ($id_hospital != "-1" && $id_test != "-1") {
                $select = $this->db->select()
			->from(array("p"=>$this->_table_name),
                                array("test_name"=>"p.name",
                                    "hospital_name"=>"p3.name",
                                    "id_hospital"=>"p.id_hospital",
                                    "reviews"=>"p.reviews",
                                    "overall"=>"p.overall",
                                    "desc"=>"p.sumary",
                                    "test_images"=>"p.images",
                                    "fee"=>"p.fee",
                                    "id_test"=>"p.id"))
                        ->join(array("p3"=>$this->_table_hospital),
                                        "p.id_hospital = p3.id")
                        ->where("p.id_hospital = ?", $id_hospital)
                        ->where("p.id = ?", $id_test)
                        ->where("p.fee <= ?", $fee)
                        ->order("p.fee DESC");
            } else if ($id_hospital != "-1") {
                $select = $this->db->select()
			->from(array("p"=>$this->_table_name),
                                array("test_name"=>"p.name",
                                    "hospital_name"=>"p3.name",
                                    "id_hospital"=>"p.id_hospital",
                                    "reviews"=>"p.reviews",
                                    "overall"=>"p.overall",
                                    "desc"=>"p.sumary",
                                    "test_images"=>"p.images",
                                    "fee"=>"p.fee",
                                    "id_test"=>"p.id"))
                        ->join(array("p3"=>$this->_table_hospital),
                                        "p.id_hospital = p3.id")
                        ->where("p.id_hospital = ?", $id_hospital)
                        ->where("p.fee <= ?", $fee)
                        ->order("p.fee DESC");
            } else if ($id_test != "-1") {
                $select = $this->db->select()
			->from(array("p"=>$this->_table_name),
                                array("test_name"=>"p.name",
                                    "hospital_name"=>"p3.name",
                                    "id_hospital"=>"p.id_hospital",
                                    "reviews"=>"p.reviews",
                                    "overall"=>"p.overall",
                                    "desc"=>"p.sumary",
                                    "test_images"=>"p.images",
                                    "fee"=>"p.fee",
                                    "id_test"=>"p.id"))
                        ->join(array("p3"=>$this->_table_hospital),
                                        "p.id_hospital = p3.id")
                        ->where("p.id = ?", $id_test)
                        ->where("p.fee <= ?", $fee)
                        ->order("p.fee DESC");
            } else {
                $select = $this->db->select()
			->from(array("p"=>$this->_table_name),
                                array("test_name"=>"p.name",
                                    "hospital_name"=>"p3.name",
                                    "id_hospital"=>"p.id_hospital",
                                    "reviews"=>"p.reviews",
                                    "overall"=>"p.overall",
                                    "desc"=>"p.sumary",
                                    "test_images"=>"p.images",
                                    "fee"=>"p.fee",
                                    "id_test"=>"p.id"))
                        ->join(array("p3"=>$this->_table_hospital),
                                        "p.id_hospital = p3.id")
                        ->where("p.fee <= ?", $fee)
                        ->order("p.fee DESC");
            }
            $data = $this->db->fetchAll($select);
            return $data;
	}
	return -1;
    }
   
    public function listAll(){
        $select = $this->db->select()
                ->from(array("p"=>$this->_table_name))
                ->where("p.active = ?",1);
	$data = $this->db->fetchAll($select);
	return $data;
    }
    public function delete(){
            $this->getById($this->_id);
            $this->_active = 0;
            return $this->update();
    }
}
?>
